<?php

namespace Common\Model;

use Org\Util\ArrayToolkit;

class CategoryModel extends BaseModel
{
    protected $num2text = array(
        'status' => array(
            0 => '禁用',
            1 => '开启',
        ),
        'display' => array(
            0 => '不可见',
            1 => '可见',
        ),
    );
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME),
    );
    protected $_validate = array(
        array('name', '', '唯一标识已经存在！', 0, 'unique', self::MODEL_BOTH),
        array('name', 'require', '请填写分类标识！'),
        array('title', 'require', '请填写分类标题！'),
    );
    protected $serializeField = array(
        'extend' => array(
            'template_index',
            'template_lists',
            'template_detail',
            'template_edit',
        ),
    );

    // 获取后代id
    // @todo:实现获取所有后代元素
    /*public function getChildIds($cid) {
        $tree = D('Tree', 'Logic');
        $data = $this->select();
        $treeD = $tree->toTree($data);
        $ids = array();
        if($child) {
            $ids = $this->loopChild($child);
        }
        $child['id'] && $ids[] = $child['id'];
        return $ids;
    }*/
    /**
     * 获取所有子代
     * @param $parent
     * @return array|mixed
     */
    public function getChilds($parent, $limit = 0)
    {
        $map = array();
        if (is_numeric($parent)) {
            $map['id'] = $parent;
        } else if (is_string($parent)) {
            $map['name'] = $parent;
        } else if (is_array($parent)) {
            $map = $parent;
        }
        $parent_row = $this->where($map)->find();
        $childs = $this->where(array(
            'pid' => $parent_row['id']
        ))->limit($limit)->select();// 获取初代后代
        $parent_ids = ArrayToolkit::column($childs, 'id');// 并作为下一次查询的父代
        while (true) {
            $parent_ids = array_unique($parent_ids);
            if (empty($parent_ids) || ($limit && $limit <= count($childs))) break; // 如果再没有父代 则跳出循环
            $m = array(
                'pid' => array('in', $parent_ids),
            );
            $res = $this->where($m)->select();
            if ($res) {
                $childs = array_merge($childs, $res);
                $parent_ids = ArrayToolkit::column($res, 'id');
            } else break;// 如果在没找到子代 则跳出循环
        }
        return $childs;
    }

    // 递归
    public function loopChild($data)
    {
        $ids = array();
        foreach ($data['_child'] as $row) {
            $row['id'] && $ids[] = $row['id'];
            if ($row['_child']) {
                $ids = array_merge($ids, $this->loopChild($row));
            }
        }
        return $ids;
    }

    /**
     * 获取所有父母
     * @param $id
     * @return array
     */
    public function getParents($id)
    {
        $cid = $id;
        $pids = array();
        while (1) {
            $res = $this->where(array('id' => $cid))->find();
            if ($res) {
                $pids[] = $res;
                $cid = $res['pid'];
            } else {
                krsort($pids); // 键倒序 使先祖排在最前面
                return $pids;
            }
        }
        krsort($pids); // 键倒序 使先祖排在最前面
        return $pids;
    }
    public function get_cate($name) {
        $map = ['name'=>$name];
        if(is_array($this->options['where'])) {
            $this->options['where'] = array_merge($this->options['where'], $map);
        }else{
            $this->options['where'] = $map;
        }
        return $this->find();
    }

    protected function _after_find(&$result, $options)
    {
        parent::_after_find($result, $options);
        if (isset($result['icon'])) {
            $result['icon_html'] = '<i class="' . $result['icon'] . '"></i>';
        }
    }
}